package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.entity.User;
import com.example.demo.qo.UserQo;
import com.example.demo.service.IUserService;
import com.example.demo.util.GenchPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;

/**
 * Created by Administrator on 2017/7/24.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    /***
     *
     *  1 跳转到登陆页面
     *  2 提交form表单到服务端
     *  3 服务端对比用户提交的数据
     *    如果正确则页面提示“登陆成功”
     *    否则登陆失败
     *
     *
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ModelAndView login(){
        return new ModelAndView("app/login");
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView loginPost(User user){
        EntityWrapper wrapper = new EntityWrapper<User>();
        wrapper.eq("login_name",user.getLoginName());
        wrapper.eq("password",user.getPassword());
        User userDB = userService.selectOne(wrapper);
        //select * from d26_user where login_name= '{0}' and password = '{1}' limit 1.format()
        if(userDB != null){
            return new ModelAndView("app/login").addObject("info","您的登陆名成功！");
        }
        return new ModelAndView("app/login").addObject("info","用户名或者密码错误！");
    }

    @RequestMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("app/index");
    }

    @RequestMapping("/registration")
    public ModelAndView registration(){
        return new ModelAndView("app/registration");
    }

    @RequestMapping("/doRegistration")
    public ModelAndView doRegistration(User user){
        //select * from d26_user where login_name = {}  limit 1
        User userDB = userService.selectOne(new EntityWrapper<User>().eq("login_name",user.getLoginName()));
        if(userDB != null){
            return new ModelAndView("app/registration").addObject("error_info","您的登陆名重复！");
        }
        userService.insert(user);
        return new ModelAndView("app/login");
    }


    @RequestMapping("/userList")
    public ModelAndView userList(UserQo userQo){

        EntityWrapper wrapper = new EntityWrapper<User>();

        if(userQo.getLoginName() != null && userQo.getLoginName().trim() != ""){
            wrapper.like("login_name",userQo.getLoginName());
        }

        if(userQo.getFirstName() != null && userQo.getFirstName().trim() != ""){
            wrapper.eq("first_name",userQo.getFirstName());
        }

        if(userQo.getId() != 0){
            wrapper.eq("id",userQo.getId());
        }

        Page<User> page  = new Page<User>(userQo.getCurrent(),10);
        userService.selectPage(page,wrapper);
        GenchPager<User> genchPager = new GenchPager<User>(page.getTotal(),userQo.getCurrent(),10);
        return new ModelAndView("app/user_list").addObject("page",page).addObject("genchPager",genchPager)
                .addObject("userQo",userQo);
    }

//@RequestParam(value = "msg",required = false) String msg
    @RequestMapping("/show")
    public ModelAndView showUser(HttpServletRequest request, int id){
        String msg = request.getParameter("msg");
        User user  = userService.selectById(id);
        return new ModelAndView("/app/show").addObject("user",user).addObject("msg",msg);
    }


    @RequestMapping("/update")
    public String update(UserQo userQo){
        User user = new User();
        user.setId(userQo.getId());
        user.setEmail(userQo.getEmail());
        user.setFirstName(userQo.getFirstName());
        user.setSecondName(userQo.getSecondName());
        userService.updateById(user);
        //return "redirect:/user/show?id="+userQo.getId()+"&msg=save success";
        return "redirect:/user/userList?id="+userQo.getId();
    }


    @RequestMapping("/delete/{id}")
    public String update(@PathVariable int id){
        userService.deleteById(id);
        return "redirect:/user/userList";
    }


}
